/*
 * Created by JFormDesigner on Fri Apr 15 19:50:50 CST 2022
 */

package com.ZengQi.event.view;

import java.awt.event.*;
import com.ZengQi.event.controller.UserFinishedViewController;
import com.ZengQi.event.entity.Finish;
import com.ZengQi.event.entity.Topic;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author QI Zeng
 */
public class UserFinishedView extends JFrame {
    private String userName;
    private List<Finish>list=null;
    private String topicName;
    private List<Topic>topicList=null;
    private  UserFinishedViewController userFinishedViewController=new UserFinishedViewController();

    /**
     * 用户的答题历史记录界面
     * @param userName
     */
    public UserFinishedView(String userName) {
        this.userName=userName;
        initComponents();
        list=userFinishedViewController.list(userName);
        if(list!=null) {
            int i = 0;
            DefaultTableModel dtm = (DefaultTableModel) topicTable.getModel();
            dtm.setRowCount(0);
            for (Finish finish : list) {
                i = i + 1;
                Vector<String> vector = new Vector<String>();
                vector.add(finish.getTopicName());
                vector.add(finish.getFinishStatus());
                dtm.addRow(vector);
            }

            int cnt = 0;
            for (int j = 0; j < list.size(); j++) {

                if (list.get(j).getFinishStatus().equals("正确")) {
                    cnt++;
                }
            }
            rightRateTxt.setText(cnt+"/"+list.size());
        }else {
            JOptionPane.showMessageDialog(this,"未找到相关记录！");

        }
        setVisible(true);
        setLocationRelativeTo(null);

    }

    private void topicTableMousePressed(MouseEvent e) {
        topicTableMousePressedActionPerformed(e);
    }

    private void topicTableMousePressedActionPerformed(MouseEvent e) {
        int row=topicTable.getSelectedRow();
        Finish finish=list.get(row);
        Topic topic=userFinishedViewController.topicList(userName).get(row);
        topicTxt.setText(topic.getTopicName());
        topicContent.setText(topic.getTopicContent());
        answerArea.setText(topic.getTopicAnswer());

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        topicTable = new JTable();
        label1 = new JLabel();
        topicTxt = new JTextField();
        label2 = new JLabel();
        scrollPane2 = new JScrollPane();
        topicContent = new JTextArea();
        label3 = new JLabel();
        rightRateTxt = new JTextField();
        label4 = new JLabel();
        scrollPane3 = new JScrollPane();
        answerArea = new JTextArea();

        //======== this ========
        setTitle("\u505a\u9898\u5e2e");
        setIconImage(new ImageIcon(getClass().getResource("/bang.png")).getImage());
        Container contentPane = getContentPane();

        //======== panel1 ========
        {

            //======== scrollPane1 ========
            {

                //---- topicTable ----
                topicTable.setModel(new DefaultTableModel(
                    new Object[][] {
                        {null, null},
                        {null, null},
                    },
                    new String[] {
                        "\u9898\u76ee\u540d\u79f0", "\u6b63\u786e/\u9519\u8bef"
                    }
                ) {
                    boolean[] columnEditable = new boolean[] {
                        false, false
                    };
                    @Override
                    public boolean isCellEditable(int rowIndex, int columnIndex) {
                        return columnEditable[columnIndex];
                    }
                });
                {
                    TableColumnModel cm = topicTable.getColumnModel();
                    cm.getColumn(0).setResizable(false);
                    cm.getColumn(0).setPreferredWidth(425);
                    cm.getColumn(1).setResizable(false);
                    cm.getColumn(1).setPreferredWidth(255);
                }
                topicTable.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        topicTableMousePressed(e);
                    }
                });
                scrollPane1.setViewportView(topicTable);
            }

            //---- label1 ----
            label1.setText("\u9898\u76ee\u540d\u79f0");

            //---- topicTxt ----
            topicTxt.setEditable(false);

            //---- label2 ----
            label2.setText("\u9898\u76ee\u5185\u5bb9");

            //======== scrollPane2 ========
            {

                //---- topicContent ----
                topicContent.setEditable(false);
                scrollPane2.setViewportView(topicContent);
            }

            //---- label3 ----
            label3.setText("\u6b63\u786e\u7387");

            //---- rightRateTxt ----
            rightRateTxt.setEditable(false);

            //---- label4 ----
            label4.setText("\u6b63\u786e\u7b54\u6848");

            //======== scrollPane3 ========
            {

                //---- answerArea ----
                answerArea.setEditable(false);
                scrollPane3.setViewportView(answerArea);
            }

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2))
                        .addGap(23, 23, 23)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addComponent(topicTxt, GroupLayout.PREFERRED_SIZE, 294, GroupLayout.PREFERRED_SIZE)
                            .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
                                .addGap(124, 124, 124))
                            .addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                                .addComponent(scrollPane3, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE)
                                .addGap(54, 54, 54))))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(85, 85, 85)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 638, GroupLayout.PREFERRED_SIZE))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addComponent(label3)
                                .addGap(36, 36, 36)
                                .addComponent(rightRateTxt, GroupLayout.PREFERRED_SIZE, 281, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                            .addComponent(topicTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4))
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addGap(42, 42, 42)
                                        .addComponent(label2))
                                    .addGroup(panel1Layout.createSequentialGroup()
                                        .addGap(32, 32, 32)
                                        .addComponent(scrollPane2, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)))
                                .addGap(38, 38, 38)
                                .addGroup(panel1Layout.createParallelGroup()
                                    .addComponent(label3)
                                    .addComponent(rightRateTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addComponent(scrollPane3, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(12, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable topicTable;
    private JLabel label1;
    private JTextField topicTxt;
    private JLabel label2;
    private JScrollPane scrollPane2;
    private JTextArea topicContent;
    private JLabel label3;
    private JTextField rightRateTxt;
    private JLabel label4;
    private JScrollPane scrollPane3;
    private JTextArea answerArea;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
